package org.hsieh.tr.explore.ui.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;
import org.hsieh.tr.explore.dbsource.IDatabaseInfo;
import org.hsieh.tr.explore.exception.ExceptionHandler;
import org.hsieh.tr.explore.ui.TransactionEditor;
import org.hsieh.tr.explore.ui.TransactionEditorInput;
import org.hsieh.tr.explore.util.JFaceUtil;

/**
 * 
 * 
 *@author hsieh 234aini@gmail.com
 * 
 */
public class OpenTranscationAction extends ActionDelegate implements
	IViewActionDelegate {

    protected IDatabaseInfo info = null;

    private IViewPart part = null;

    @Override
    public void run(IAction action) {
	try {
	    PlatformUI.getWorkbench().getActiveWorkbenchWindow()
		    .getActivePage().openEditor(
			    TransactionEditorInput.newInput(info),
			    TransactionEditor.ID, true);
	} catch (PartInitException e) {
	    ExceptionHandler.prompt(part.getSite().getShell(), e);
	} catch (Exception e) {
	    ExceptionHandler.prompt(part.getSite().getShell(), e);
	}
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
	info = JFaceUtil.getFristSelected(selection, IDatabaseInfo.class);
    }

    public void init(IViewPart view) {
	part = view;
    }
}
